package cn.cnsuh.dao.impl;


import cn.cnsuh.dao.UserDao;
import cn.cnsuh.domain.User;
import cn.cnsuh.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * @author Magic
 * @create 2020-05-09 11:30
 */
public class UserDaoImpl implements UserDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public User findByUsernameAndPassword(String username, String password) {
        User user = null;

        try {
            String sql = " select * from tab_user where username = ? and password = ? ";

            user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), username, password);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }

        return user;
    }


    @Override
    public User findByUsername(String username) {
        User user = null;
        try {
            //1.定义sql
            String sql = " select * from tab_user where username = ? ";
            //2.执行sql
            user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), username);
        } catch (Exception e) {

        }
        return user;//如果有查询到值则返回user的值否则则返回null，如果不try catch 查询不到值会直接异常不会返回null
    }

    @Override
    public void save(User user) {
        //1.定义sql
        String sql = " insert into tab_user (username,password,realname,birthday,sex,telephone,email,status,code)" +
                " values(?,?,?,?,?,?,?,?,?) ";

        //2.执行sql
        template.update(sql, user.getUsername(),
                user.getPassword(),
                user.getRealname(),
                user.getBirthday(),
                user.getSex(),
                user.getTelephone(),
                user.getEmail(),
                user.getStatus(), user.getCode());
    }


    /**
     * 根据激活码查询用户对象
     *
     * @param code
     * @return
     */
    @Override
    public User findByCode(String code) {

        User user = null;
        try {
            String sql = " select * from tab_user where code = ? ";

            user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), code);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }

        return user;
    }

    /**
     * 修改制定用户激活状态
     *
     * @param user
     */
    @Override
    public void updateStatus(User user) {
        String sql = " update tab_user set status = 'Y' where uid=? ";

        template.update(sql, user.getUid());
    }

    @Override
    public void updateU(User user,int uid) {
//        String sql = " update student set sname = ? , gender = ? , sclass = ? , birth = ? , grade = ?  where sno = ?";
//
//        template.update(sql, student.getSname(), student.getGender(), student.getSclass(), student.getBirth(),
//                student.getGrade(), student.getSno());

        String sql = " update tab_user set username = ? , realname = ?  , birthday = ? , sex = ? , telephone = ? , email = ? where uid = ? ";

        template.update(sql,user.getUsername(),user.getRealname(),user.getBirthday(),user.getSex(),user.getTelephone(),user.getEmail(),uid);
    }

    @Override
    public List<User> findAll() {
        String sql = " select * from tab_user ";

        List<User> users = template.query(sql, new BeanPropertyRowMapper<User>(User.class));

        return users;
    }

    @Override
    public void delete(int uid) {
        // 1、定义sql
        String sql = "delete from tab_user where uid = ?";
        // 2、执行sql
        template.update(sql, uid);
    }


}
